package org.halk.bio;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * bio测试
 * block i/o  java传统同步阻塞io
 * <p>
 * socket客户端可以用telnet命令，telnet 127.0.0.1 8987
 * ctrl + ]
 * send  hello
 *
 * @author halk
 * @date 2023/4/10
 */
public class BioServerDemo {

    /**
     * 1、创建线程池
     */
    static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            5,
            10,
            1,
            TimeUnit.MINUTES,
            new ArrayBlockingQueue<>(10),
            new ThreadFactoryBuilder().setNameFormat("myThread-pool-%d").build()
    );

    @Test
    public void bioTest() throws IOException {
        //线程池机制
        ServerSocket serverSocket = new ServerSocket(8987);
        System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " server start ...");

        while (true) {
            System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " socket等待连接中");
            final Socket socket = serverSocket.accept();
            System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " 连接到一个客户端");
            System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " socket已经建立连接，执行后续操作");
            threadPoolExecutor.execute(() -> handler(socket));
        }
    }

    public static void handler(Socket socket) {
        try {
            System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " 进入hangler处理方法中了");
            InputStream inputStream = socket.getInputStream();
            while (true) {
                byte[] bytes = new byte[1024];
                System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " socket等待读取数据");
                int read = inputStream.read(bytes);
                System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " socket数据已经全部读取完成");
                if (read != -1) {
                    System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + new String(bytes, 0, read));
                } else {
                    System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + " 循环break");
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
                System.out.println(Thread.currentThread().getId() + "-" + Thread.currentThread().getName() + "客户端关闭。。。");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
